define(["moment"], function (moment) {
	return ["$scope", "$filter", "$stateParams", "config", "MMWService", "$rootScope",
	function ($scope, $filter, $stateParams, config, $MMWService, $rootScope) {
		$scope.selectItemNumber = 0;
		$scope.plotList = [];
		$scope.isSelectAll = false;
		$scope.init = function(){
			$scope.item_no = '';
			$scope.wo_no = '';
			//$MMWService.PNIGList = {};
		};

		$scope.back = function(){
			$scope.$root.hideMenu();
			$scope.$root.changeProgram('home', true);
		};

		$scope.itemClick = function(index, item){
			item.isSelect = !item.isSelect;
			if(item.isSelect){
				$scope.selectItemNumber++;
			} else {
				$scope.selectItemNumber--;
			}
		};

		$scope.barcodeScan = function(event){
			if(event.keyCode == 13){
				$scope.search();
			}
		};

		$scope.loadItemNoList = function(){
			$MMWService.showQueryListPNIG({
				page_mode: 5,
				key:'page_no',
                pageRecord:50,
				selectCode:$scope.item_no,
				condition: [
					{name:$filter('translate')('common.queryList.item_c'), column_name:'ProductNo', query_mode:'2', value:$scope.item_no}
				],
				header: [
					{label:$filter('translate')('common.queryList.item_c'), code:'page_no'},
					{label:$filter('translate')('common.queryList.item_n'), code:'page_name'}
				],
				confirm : function(selectItem){
					$scope.item_no = selectItem.page_no;
				}
			});
		};

		$scope.loadWoNoList = function(){
			$MMWService.showQueryListPNIG({
				page_mode: 1,
                pageRecord:50,
				condition: [
					{name:$filter('translate')('common.queryList.wo_c'), column_name:'MONo', query_mode:'2', value:$scope.wo_no}
				],
				header: [
					{label:$filter('translate')('common.queryList.wo_c'), code:'page_no'},
                    {label:$filter('translate')('common.queryList.item_c'), code:'productno'},
                    {label:$filter('translate')('common.queryList.item_n'), code:'productname'},
                    {label:$filter('translate')('common.queryList.wo_no_qty'), code:'moqty', width:'110px'}
				],
				selectCode:$scope.wo_no,
				setCondition:function(conditions){
                    conditions.push({column_name:'MOSTATE', query_mode:'0', value:'3', merge_condition_model:'AND' });
					if($scope.item_no && $scope.item_no != '')
						conditions.push({column_name:'A.productno', query_mode:'2', value:$scope.item_no, merge_condition_model:'AND' });

					return conditions;
				},
				confirm : function(selectItem){
					$scope.wo_no = selectItem.page_no;
				}
			});
		};

		$scope.search = function(feedback){
			if($scope.item_no != '' || $scope.wo_no != ''){
				$MMWService.sendToServer({
					uri: 'ServicesSTD.Module_WIP.plot_delete_info_get',
					content : {
						wo_no : $scope.wo_no,//工單號碼
						item_no : $scope.item_no//產品編號
					},
					success : function(data){
						//20220915 modify by wangqq for M#0110347：生产批删除模块增加派工状态栏位和卡控
						data.plot_delete_info_get.forEach(function(temp){
							temp.dispatch_state_name = get_dispatch_state(temp.dispatch_state)
						});
						$scope.plotList = data.plot_delete_info_get;
						$scope.selectItemNumber = 0;
						$scope.isSelectAll = false;

						if(feedback){
                            feedback();
                        } else if(!data.plot_delete_info_get || data.plot_delete_info_get.length <= 0){
                            $scope.$root.showAlert($filter('translate')('common.msg.no_data'));
                        }
					}
				});
			} else {
				$scope.showAlert($filter('translate')('B0103.msg.searchEmpty'));
			}
		};

		//20220915 add by wangqq for M#0110347:派工状态多国语系转化
 		function get_dispatch_state(type) {
			var str = "";
			switch (type) {
				case 0:
					str = $filter('translate')('B0103.state_list.state_0');
					break;
				case 1:
					str = $filter('translate')('B0103.state_list.state_1');
					break;
				}
				return str;
			};

		$scope.confirm = function(){
			var plot_delete_info_process = [];
			var isConfirm=true;
			var plotlist='';
			// 20230206 modify by Alan for#127226 : 使用nodeRequire應檢查當前平台
			//20220812 add by 王晴晴 for M#0105944:  生产批删除成功，记录相关信息到TBLWIPDeleteLotLog表
			//获取本机电脑名称
			var hostname = '';
			if($rootScope.isElectron) {
				hostname = window.nodeRequire('os').hostname();
			} else {
				hostname = 'web';
			}
			$scope.plotList.forEach(function(plot){
				if(plot.isSelect){
					plot_delete_info_process.push(plot);
				}
				if(plot.isSelect && plot.dispatch_state==1){
					isConfirm=false;
					plotlist+=plot.plot_no+'、';
					plot_list=plotlist.substring(0,plotlist.length-1);
				}
			});

			if (isConfirm==false){ 
				$scope.$root.showAlert($filter('translate')('B0103.msg.failed',{plot_no: plot_list}));
			}
			else {
				$MMWService.sendToServer({
					uri: 'ServicesSTD.Module_WIP.plot_delete_info_process',
					content : {
						polt_detail : plot_delete_info_process,
						hostname:hostname
					},
					success : function(data){
						//修正 生產批刪除後，需顯示查詢按鈕跟取消全選
						$scope.isSelectAll = false;
						$scope.selectItemNumber = 0;
						$scope.search(function(){							
							$scope.$root.showToast($filter('translate')('B0103.msg.success'));
						});
					}
				});
			}
		};

		$scope.selectAll = function(){
			if($scope.plotList.length >0){
				$scope.isSelectAll = !$scope.isSelectAll;
				$scope.plotList.forEach(function(plotItem){
					plotItem.isSelect = $scope.isSelectAll;
				});

				if($scope.isSelectAll){
					$scope.selectItemNumber = $scope.plotList.length;
				} else {
					$scope.selectItemNumber = 0;
				}
			}
		};
	}
];});